package com.hz.foodstalls.controller;

import org.apache.commons.lang3.StringUtils;

import com.jfinal.plugin.activerecord.Page;

import com.hz.foodstalls.interceptor.AdminInterceptor;

import com.hz.foodstalls.controller.BaseController;

import com.jfinal.aop.Before;import com.jfinal.ext.route.ControllerBind;
import java.util.HashMap;
import java.util.Map;
import com.hz.foodstalls.model.WechatRobotMedia;

import com.hz.foodstalls.validator.WechatRobotMediaValidator;

/**
 * WechatRobotMediaController, Generated by Robot on 2017-8-25 15:39:45.
 */
@Before(AdminInterceptor.class)
@ControllerBind(controllerKey = "/wechatrobotmedia", viewPath = "/WEB-INF/views/wechatrobotmedia/")
public class WechatRobotMediaController extends BaseController {

	private final String MODULE_NAME = "/wechatrobotmedia";


	public void index() {
		String id = getPara("id");
		Page<WechatRobotMedia> page = WechatRobotMedia.dao.page(getParaToInt("pageNumber", 1), getParaToInt("pageSize", 10),id);
		setAttr("page", page);
		setAttr("id",id);
		render("index.html");
	}

	@Before(WechatRobotMediaValidator.class)
	public void save() {
		WechatRobotMedia model = getModel(WechatRobotMedia.class, "wechatRobotMedia");
		boolean isSave = model.getId() == null;

		if (WechatRobotMedia.dao.saveOrUpdate(model, isSave));
			//redirect(MODULE_NAME+"/?id="+model.getWechatRobotId());
		else
			renderError(501);
	}

	public void edit() {
		int flag = Integer.parseInt(getPara("flag"));
		int wechat_robot_id=Integer.parseInt(getPara("robotid"));
		WechatRobotMedia wechatRobotMedia = WechatRobotMedia.dao.findById(getParaToInt("id",0));
		if (wechatRobotMedia == null)
			wechatRobotMedia = new WechatRobotMedia();
		wechatRobotMedia.put("wechat_robot_id",wechat_robot_id);

		setAttr("flag",flag);
		setAttr("wechatRobotMedia", wechatRobotMedia);
		render("edit.html");
	}

	public void delete() {
		String ids = StringUtils.join(getParaValues("ids"), ",");
		int ret = WechatRobotMedia.dao.batchDelete(ids);
		if (ret>0) {
			ajaxJsonSuccessMessage("删除成功!");
		} else {
			ajaxJsonErrorMessage("删除失败！");
		}
	}

	private String indexTitle = "微信自动回复素材管理";
	private String editTitle = "编辑";

	public Map<String,String> getPageTitleBread() {
		Map<String,String> pageTitleBread = new HashMap<String,String>();
		pageTitleBread.put("indexTitle", indexTitle);
		pageTitleBread.put("editTitle", editTitle);
		return pageTitleBread;
	}
}
